/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.document;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.pojo.*;
import com.documents.service.country.CountryService;
import com.documents.service.product.ProductService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class DocumentService extends BaseSrv<Document, Integer> {

    private DocumentGroupService documentGroupService;
    private ProductService productService;
    private CountryService countryService;

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public DocumentGroupService getDocumentGroupService() {
        return documentGroupService;
    }

    public void setDocumentGroupService(DocumentGroupService documentGroupService) {
        this.documentGroupService = documentGroupService;
    }

    public PageBean getPageBean(int page, int pageSize, Document document, String groupname) {
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";

        int paramIndex = 0;
        Object[] parameters = new Object[12];
        if (groupname != null && !groupname.equals("")) {
            DocumentGroup dg = documentGroupService.findUnique("name=?", new Object[]{groupname.trim()});
            if (dg.getLevel() == 1) {
                List<DocumentGroup> dg2 = this.documentGroupService.find("pid=?", new Object[]{dg.getId()}, "id");
                List<DocumentType> typeslist = new ArrayList();
                for (DocumentGroup o : dg2) {
                    Set types = o.getDocumentTypes();
                    typeslist.addAll(types);
                }
                if (typeslist != null && typeslist.size() > 0) {
                    int size = typeslist.size();
                    parameters = new Object[size + 12];
                    condition += "(";
//                    int paramIndex = 0;
                    for (DocumentType t : typeslist) {
                        condition += "type=? or ";
                        parameters[paramIndex] = t.getId();
                        paramIndex++;
                    }
                }
            }
            if (dg.getLevel() == 2) {
                Set types = dg.getDocumentTypes();
                List<DocumentType> typeslist = new ArrayList(types);
                if (typeslist != null && typeslist.size() > 0) {
                    int size = typeslist.size();
                    parameters = new Object[size + 12];
                    condition += "(";
//                    int paramIndex = 0;
                    for (DocumentType t : typeslist) {
                        condition += "type=? or ";
                        parameters[paramIndex] = t.getId();
                        paramIndex++;
                    }
                }
            }
            condition = condition.substring(0, condition.lastIndexOf("or"));
            condition += ")";

        }



        if (document != null) {

            if (document.getCode() != null && !document.getCode().equals("")) {
                if (condition.trim().length() == 0) {
                    condition += "code=?";
                } else {
                    condition += " and code=?";
                }

                parameters[paramIndex] = document.getCode();
                paramIndex++;
            }
            if (document.getProduct() != null && document.getProduct().getId() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " product_code like ?";

                } else {
                    condition += " and product_code like ?";
                }
                Product pp = productService.get(document.getProduct().getId());

                parameters[paramIndex] = pp.getCode() + "%";
                paramIndex++;
            }
//
//            if (document.getProductName() != null && !document.getProductName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += " product_name=?";
//
//                } else {
//                    condition += " and product_name=?";
//                }
//                parameters[paramIndex] = document.getProductName();
//                paramIndex++;
//            }



            if (document.getDocumentType() != null && document.getDocumentType().getId() != null && document.getDocumentType().getId().intValue() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " type=?";
                } else {
                    condition += " and type=?";
                }
                parameters[paramIndex] = document.getDocumentType().getId();
                paramIndex++;
            }
//            if (document.getCountryName() != null && !document.getCountryName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += " country_name=?";
//                } else {
//                    condition += " and country_name=?";
//                }
//                parameters[paramIndex] = document.getCountryName();
//                paramIndex++;
//            }
            if (document.getTypeName() != null && !document.getTypeName().equals("")) {
                if (condition.trim().length() == 0) {
                    condition += " type_name=?";
                } else {
                    condition += " and type_name=?";
                }
                parameters[paramIndex] = document.getTypeName().trim();
                paramIndex++;
            }

            if (document.getName() != null && !document.equals("")) {
                if (condition.trim().length() == 0) {
                    condition += " name like ?";

                } else {
                    condition += " and name like ?";
                }
                parameters[paramIndex] = "%" + document.getName().trim() + "%";
                paramIndex++;
            }
            Long allRow = this.findTotalCount(condition, parameters);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Document> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }

            pageBean.setList(list);

        } else {
            Long allRow = this.findTotalCount("", null);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Document> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }

            pageBean.setList(list);
        }
        return pageBean;
    }

    public List<Document> getDocumentList() {

        List<Document> list = this.find("", null, "id");
        return list;

    }

    public PageBean getPageBean(int page, int pageSize, int groupid) {



        PageBean pageBean = new PageBean();
        try {
          
      

                DocumentGroup dg = documentGroupService.findUnique("id=?", new Object[]{groupid});
                if (dg == null) {
                    return pageBean;
                }
                if (dg.getLevel() == 1) {
                    List<DocumentGroup> dg2 = this.documentGroupService.find("pid=?", new Object[]{dg.getId()}, "id");
                    List<DocumentType> typeslist = new ArrayList();
                    for (DocumentGroup o : dg2) {
                        Set types = o.getDocumentTypes();
                        typeslist.addAll(types);

                    }
                    pageBean.setPageSize(pageSize);
                    pageBean.setCurrentPage(page);

                    if (typeslist != null && typeslist.size() > 0) {
                        int size = typeslist.size();

                        Object[] parameters = new Object[size];

                        int paramIndex = 0;
                        String sql = "";

                        for (DocumentType t : typeslist) {

                            sql += "type=? or ";
                            parameters[paramIndex] = t.getId();
                            paramIndex++;

                        }
                        sql = sql.substring(0, sql.lastIndexOf("or"));
                        Long allRow = this.findTotalCount(sql, parameters);
                        int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
                        pageBean.setAllRow(allRow.intValue());
                        pageBean.setTotalPage(totalPage);
                        List<Document> list = this.findWithPaging(sql, parameters, "id", page - 1, pageSize);
                        pageBean.setList(list);
                    }

                }
                if (dg.getLevel() == 2) {
                    pageBean.setPageSize(pageSize);
                    pageBean.setCurrentPage(page);
                    Set types = dg.getDocumentTypes();
                    List<DocumentType> typeslist = new ArrayList(types);
                    if (typeslist != null && typeslist.size() > 0) {
                        int size = typeslist.size();
                        Object[] parameters = new Object[size];

                        int paramIndex = 0;
                        String sql = "";

                        for (DocumentType t : typeslist) {

                            sql += "type=? or ";
                            parameters[paramIndex] = t.getId();
                            paramIndex++;

                        }
                        sql = sql.substring(0, sql.lastIndexOf("or"));
                        Long allRow = this.findTotalCount(sql, parameters);
                        int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
                        pageBean.setAllRow(allRow.intValue());
                        pageBean.setTotalPage(totalPage);
                        List<Document> list = this.findWithPaging(sql, parameters, "code", page - 1, pageSize);
                        pageBean.setList(list);
                    }
                }
      

            return pageBean;
        } catch (Exception e) {
            return pageBean;
        }

    }
}
